package com.boe.hbd.client.overlaymanager;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

public class TitleMarkerRenderer implements MarkerRenderer {
	private int arcR = 8;
	private int charsEachLine = 12;
	private int sizeOfFont = 16; 

	public TitleMarkerRenderer( ) {
		this(8, 12, 16);
	}

	public TitleMarkerRenderer(int arcR, int charsEachLine, int sizeOfFont) {
		super();
		this.arcR = arcR;
		this.charsEachLine = charsEachLine;
		this.sizeOfFont = sizeOfFont;
	}

	public Drawable render(ManagedOverlayItem item, Drawable marker, int bitState) {
		if (item.getCustomRenderedDrawable() != null) {
			return item.getCustomRenderedDrawable();
		}
		if (item.getIcon() != null) {
			marker = item.getIcon();
		}
		int iconW = marker.getIntrinsicWidth();
		int widthOfLine = 0;
		int heightOfChar = 0;
		Paint tp = new Paint();
		tp.setTextSize(sizeOfFont);
		tp.setAntiAlias(true);
		tp.setColor(Color.BLACK);
		// STEP 1: calculate lines of text
		List<String> textLines = textToLines(item.getTitle(), charsEachLine);
		{
			Rect textR = new Rect();
			for (String strLine : textLines) {
				tp.getTextBounds(strLine, 0, strLine.length(), textR);
				if (textR.height() > heightOfChar) {
					heightOfChar = textR.height();
				}
				if (textR.width() > widthOfLine) {
					widthOfLine = textR.width();
				}
			}
		}
		int heightOfInfoBox = textLines.size() * heightOfChar + (textLines.size() - 1) * 2 + arcR * 2 ;
		int widthOfInfoBox = widthOfLine + arcR * 2 + iconW ;
		if (marker != null && marker.getIntrinsicHeight() > heightOfInfoBox) {
			heightOfInfoBox = marker.getIntrinsicHeight();
		}
		// STEP 2: create the bitmap
		Bitmap bitmap = Bitmap.createBitmap(widthOfInfoBox, heightOfInfoBox, Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas(bitmap);
		Paint paint = new Paint();
		paint.setAntiAlias(true);
		// STEP 3: draw the icon on demand
		canvas.drawBitmap(((BitmapDrawable) marker).getBitmap(), 0, 0, paint);
		// STEP 4: draw the arcR
		paint.setARGB(255, 255, 251, 239);
		canvas.drawRoundRect(new RectF(iconW, 0, widthOfInfoBox, heightOfInfoBox), arcR, arcR, paint);
		paint.setARGB(255, 198, 195, 198);
		paint.setStyle(Paint.Style.STROKE);
		paint.setStrokeWidth(2);
		canvas.drawRoundRect(new RectF(iconW, 0, widthOfInfoBox, heightOfInfoBox), arcR, arcR, paint);
		// STEP 5: draw the text
		int textY = heightOfChar + arcR;
		for (String strLine : textLines) {
			canvas.drawText(strLine, arcR + iconW, textY, tp);
			textY += heightOfChar + 2;
		}
		BitmapDrawable bitmapFinal = new BitmapDrawable(bitmap);
		bitmapFinal.setBounds(0, 0, bitmapFinal.getIntrinsicWidth(), bitmapFinal.getIntrinsicHeight());
		item.setCustomRenderedDrawable(bitmapFinal);
		return bitmapFinal;
	}

	public List<String> textToLines(String text, int charsEachLine) {
		List<String> textLines = new ArrayList<String>();
		StringBuffer strLine = new StringBuffer();
		for (int i = 0; i < text.length(); i++) {
			if (text.charAt(i) == '\n') {
				if (strLine.length() > 0) {
					textLines.add(strLine.toString());
				}
				strLine = new StringBuffer();
			} else {
				strLine.append(text.charAt(i));
				if (strLine.length() == charsEachLine) {
					textLines.add(strLine.toString());
					strLine = new StringBuffer();
				}
			}
		}
		if (strLine.length() > 0) {
			textLines.add(strLine.toString());
		}
		return textLines;
	}

}
